package com.macro.mall.common.mybatis;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 自动填充日期
 *
 * @author 11519
 * @date 2023年 04月28日 22:37:01
 */
@Component
@Slf4j
public class DataFillHandler implements MetaObjectHandler {

    public static final String CREATE_BY = "createBy";
    public static final String CREATE_AT = "createAt";
    public static final String UPDATE_AT = "updateAt";
    public static final String CREATE_TIME = "createTime";

    public static final String UPDATE_TIME = "updateTime";
    public static final String UPDATE_BY = "updateBy";

    @Override
    public void insertFill(MetaObject metaObject) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal != null){
            // 先判断字段是否存在
            boolean createBy = metaObject.hasSetter(CREATE_BY);
            if (createBy && metaObject.getValue(CREATE_BY) == null) {
               this.strictInsertFill(metaObject, CREATE_BY, String.class, principal.getUsername());
            }

            boolean updateBy = metaObject.hasSetter(UPDATE_BY);
            if (updateBy && metaObject.getValue(UPDATE_BY) == null) {
                this.strictInsertFill(metaObject,UPDATE_BY,String.class,principal.getUsername());
            }

            boolean createAt = metaObject.hasSetter(CREATE_AT);
            if (createAt && metaObject.getValue(CREATE_AT) == null) {
                this.strictInsertFill(metaObject,CREATE_AT, Date.class,new Date());
            }

            boolean updateAt = metaObject.hasSetter(UPDATE_AT);
            if (updateAt && metaObject.getValue(UPDATE_AT) == null) {
                this.strictInsertFill(metaObject,UPDATE_AT,Date.class,new Date());
            }

            boolean createTime = metaObject.hasSetter(CREATE_TIME);
            if (createTime && metaObject.getValue(CREATE_TIME) == null) {
                this.strictInsertFill(metaObject,CREATE_TIME,Date.class,new Date());
            }

            boolean updateTime = metaObject.hasSetter(UPDATE_TIME);
            if (updateTime && metaObject.getValue(UPDATE_TIME) == null) {
                this.strictInsertFill(metaObject,UPDATE_TIME,Date.class,new Date());
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal != null) {
            boolean updateBy = metaObject.hasSetter(UPDATE_BY);
            if (updateBy) {
                this.strictInsertFill(metaObject, UPDATE_BY,String.class,principal.getUsername());
            }

            boolean updateAt = metaObject.hasSetter(UPDATE_AT);
            if (updateAt) {
                this.strictInsertFill(metaObject,UPDATE_AT,Date.class,new Date());
            }
            boolean updateTime = metaObject.hasSetter(UPDATE_TIME);
            if (updateTime) {
                this.strictInsertFill(metaObject, UPDATE_TIME,Date.class,new Date());
            }
        }
    }
}
